package com.glwan.fixedassetapp.utils;


import com.google.gson.JsonObject;

import org.apache.xerces.impl.dv.util.Base64;

import java.nio.charset.Charset;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * okHttp 工具类
 */
public class OkHttpUtil {

    private static final String BASE_URL = "https://oa.fgw.sz.gov.cn/ekp";

    private static final String USERNAME = "test";

    private static final String PASSWORD = "test";

    private static final String AUTHORIZATION = "Basic " + Base64.encode((USERNAME + ":" + PASSWORD).getBytes(Charset.forName("UTF-8")));

    /**
     * 支持ssl
     */
    private static final OkHttpClient client = new OkHttpClient.Builder()
            .followRedirects(true)
            .followSslRedirects(true)
            .connectTimeout(2, TimeUnit.MINUTES)
            .readTimeout(2, TimeUnit.MINUTES)
            .writeTimeout(2, TimeUnit.MINUTES)
            .build();

    /**
     * post请求
     *
     * @param url       url
     * @param headerMap 头信息map
     * @param callback  回调函数
     */
    public static void get(String url, Map<String, Object> headerMap, Callback callback) {
        Request request = new Request.Builder()
                .url(BASE_URL + url)
                .headers(getHeaders(headerMap))
                .build();

        Call call = client.newCall(request);
        call.enqueue(callback);
    }

    /**
     * post请求
     *
     * @param url        url
     * @param headerMap  头信息map
     * @param jsonObject 数据map
     * @param callback   回调函数
     */
    public static void post(String url, Map<String, Object> headerMap, JsonObject jsonObject, Callback callback) {
        Request request = new Request.Builder()
                .url(BASE_URL + url)
                .headers(getHeaders(headerMap))
                .post(getRequestBody(jsonObject))
                .build();

        Call call = client.newCall(request);
        call.enqueue(callback);
    }

    private static RequestBody getRequestBody(JsonObject jsonObject) {
        return FormBody.create(jsonObject.toString(), MediaType.parse("application/json; charset=utf-8"));
    }

    private static Headers getHeaders(Map<String, Object> headerMap) {

        Headers.Builder headerBuilder = new Headers.Builder();

        for (Map.Entry<String, Object> item : headerMap.entrySet()) {

            headerBuilder.add(item.getKey(), String.valueOf(item.getValue()));

        }
        headerBuilder.add("Authorization", AUTHORIZATION);

        return headerBuilder.build();
    }

    public static void postNew(String url, String meidaType, Map<String, Object> headerMap, JsonObject jsonObject, Callback callback) {
        MediaType mediaType = MediaType.parse(meidaType);
        RequestBody body = RequestBody.create(mediaType, null == jsonObject ? "" : jsonObject.toString());
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .headers(getHeadersNew(headerMap))
                .build();
        client.newCall(request).enqueue(callback);
    }

    private static Headers getHeadersNew(Map<String, Object> headerMap) {

        Headers.Builder headerBuilder = new Headers.Builder();

        for (Map.Entry<String, Object> item : headerMap.entrySet()) {

            headerBuilder.add(item.getKey(), String.valueOf(item.getValue()));

        }
        return headerBuilder.build();
    }
}
